package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.app.qbake.R;
import com.app.qbake.module.mine.adapter.OrderListBuyerAdapter;
import com.app.qbake.module.mine.adapter.OrderListSellerAdapter;
import com.app.qbake.module.mine.di.OrderListComponent;
import com.app.qbake.module.mine.event.H5PaySuccessEvent;
import com.app.qbake.module.mine.mvp.OrderListView;
import com.app.qbake.module.mine.presenter.OrderListPresenter;
import com.wind.base.adapter.BaseDelegateRecyclerAdapter;
import com.wind.base.mvp.view.BasePageFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.PrefsUtil;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.OrderListRequest;
import com.wind.data.mine.OrderListResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import javax.inject.Inject;

/**
 * Created by wind on 2018/10/20.
 */

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.OrderListView",
                responseCanonicalName = "com.wind.data.mine.OrderListResponse",
                requestCanonicalName = "com.wind.data.mine.OrderListRequest",
                page = true

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "order/getAll")
)
public class OrderListFragment extends BasePageFragment<OrderListView, OrderListPresenter,
        OrderListComponent, OrderListResponse> implements OrderListView {

    public static final String ARGS_KEY_TYPE = "args_key_type";
    public static final String ARGS_KEY_STATUS = "args_key_status";
    public static final String ARGS_KEY_MAIL = "args_key_mail";
    public static final String PREF_KEY_GOODS_MAIL = "pref_key_goods_mail";
    @Inject
    OrderListPresenter presenter;

    private String mType;
    private String mStatus;
    private int mail;//对于已支付的订单卖家是否已经发货

    @Override
    public OrderListPresenter createPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mType = getArguments().getString(ARGS_KEY_TYPE);
        mStatus = getArguments().getString(ARGS_KEY_STATUS);
        mail = getArguments().getInt(ARGS_KEY_MAIL);
        PrefsUtil.setInt(getActivity(), PREF_KEY_GOODS_MAIL, mail);
        EventBus.getDefault().register(this);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected BaseDelegateRecyclerAdapter getAdapter() {
        if (mType == OrderListRequest.TYPE_BUY) {
            OrderListBuyerAdapter adapter = new OrderListBuyerAdapter(getActivity());
            return adapter;
        } else {
            OrderListSellerAdapter adapter = new OrderListSellerAdapter(getActivity());
            return adapter;
        }
    }

    @Override
    protected BaseRequest buildRequest(boolean isFirstPage) {
        OrderListRequest request = new OrderListRequest();
        request.setFirstPage(isFirstPage);
        request.setType(mType);
        request.setOrder_status(mStatus);

        request.setIs_mail(mail);
        return request;
    }

    public static OrderListFragment getInstance(String type, String status, int mail) {
        OrderListFragment f = new OrderListFragment();
        Bundle args = new Bundle();
        args.putString(ARGS_KEY_TYPE, type);
        args.putString(ARGS_KEY_STATUS, status);
        args.putInt(ARGS_KEY_MAIL, mail);
        f.setArguments(args);
        return f;
    }

    public void refresh() {
        if (presenter != null)
            loadFirstPage();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    /**
     * 支付宝网页支付成功回调
     *
     * @param event
     */
    @Subscribe
    public void onH5PaySuccessEvent(H5PaySuccessEvent event) {
        refresh();
    }
}
